﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrBitDataModule.Common
{
    public static class StringUtils
    {
        /// <summary>
        /// 获取所有编码名称
        /// </summary>
        /// <returns></returns>
        public static IList<string> GetAllEncodingNames() => Encoding.GetEncodings().Select(c => c.Name).ToList();

        /// <summary>
        /// 根据编码名称获取编码对象
        /// </summary>
        /// <param name="encodingName"></param>
        /// <returns></returns>
        public static Encoding GetEncoding(string encodingName) => Encoding.GetEncoding(encodingName);


        /// <summary>
        /// 将字符串转换为 Base64 编码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string StringToBase64(string input)
        {
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input);
            return Convert.ToBase64String(bytes);
        }

        /// <summary>
        /// 将 Base64 编码转换为字符串
        /// </summary>
        /// <param name="base64"></param>
        /// <returns></returns>
        public static string Base64ToString(string base64)
        {
            byte[] bytes = Convert.FromBase64String(base64);
            return System.Text.Encoding.UTF8.GetString(bytes);
        }
    }
}
